package oop.ex3.messages;

import java.io.IOException;
import java.net.InetSocketAddress;

import oop.ex3.sockets.ClientSocket;

public class PeerInfoMessage extends SimpleMessage {
	
	public static String[] _headers = {ValidHeaders.CONTAINNAMESERVER, 
		ValidHeaders.FILEADDRESS, ValidHeaders.BEGIN};
	
	String _ip;
	int _port;
	InetSocketAddress _peer;
	
	public PeerInfoMessage(String msgName, InetSocketAddress info){
		super(msgName);
		_ip = info.getAddress().getHostAddress();
		_port = info.getPort();
		_peer = info;
	}
	
	public PeerInfoMessage(String msgName){
		super(msgName);
	}

	public String getIP() {
		return _ip;
	}
	
	public int getPort(){
		return _port;
	}
	
	public InetSocketAddress getPeer(){
		return _peer;
	}
	
	public void sendMessage(ClientSocket socket) throws IOException{
		System.out.println("OUT <to: "+socket.getPort()+"> "+_header);
		socket.writeToSocket(_header);
		socket.writeToSocket(_ip);
		socket.writeToSocket(_port);
	}
	
	public PeerInfoMessage readMessage(ClientSocket socket) 
	throws IOException{
		System.out.println("IN <from: "+socket.getPort()+"> "+_header);
		_ip = socket.readUTF();
		//TODO
		int _port = socket.readInt();
		_peer = new InetSocketAddress(_ip,_port);
		//read end
		socket.readUTF();
		return this;
	}

	
}
